<!DOCTYPE html>
<html lang="en">
<title>优惠券领取记录</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">优惠券领取记录</h5>
                </div>
                <div class="ibox-content">

                    <div class="table-responsive">
                        <form class="form-inline" role="form">
                            <input type="hidden" id="couponId" value="${couponId!}">
                            <div class="form-group margin-left">
                                <label for="userId">用户ID：</label>
                                <input class="form-control" id="userId" type="text" placeholder="请输入用户ID"/>
                            </div>
                            <button id="search" type="button" class="btn btn-outline btn-primary" style="margin-top: 5px">确认</button>
                        </form>

                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>券码</th>
                                <th>领取人ID</th>
                                <th>领取人昵称</th>
                                <th>用户身份</th>
                                <th>领取时间</th>
                                <th>使用时间</th>
                            </tr>
                            </thead>
                            <tbody id="couponGetList">
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="ibox-content overflow">
                    <div class="col-sm-12 padding-left-0">
                        <h4 class="pull-left">合计：<span id="couponGetCount">0</span>人领取，<span id="couponUsedCount">0</span>人使用
                        </h4>
                        <div id="copot-page" class="pull-right"></div>
                    </div>
                    <button type="button" id="go_back_list" class="btn btn-primary pull-left">返回列表</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    //返回列表
    $('#go_back_list').click(function () {
        window.location = "${baseUrl}system/coupon/to_list";
    });
    //搜索
    $("#search").click(function () {
        coursePage(1)
    });

    $(document).ready(function () {

        Date.prototype.pattern = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
                "H+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            var week = {
                "0": "/u65e5",
                "1": "/u4e00",
                "2": "/u4e8c",
                "3": "/u4e09",
                "4": "/u56db",
                "5": "/u4e94",
                "6": "/u516d"
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            if (/(E+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                }
            }
            var str = fmt.lastIndexOf("N");
            if (str != -1) {
                return "";
            }

            return fmt;
        }

        coursePage(1);
    });

    function coursePage(currentIndex) {
        var param = {
            "pageIndex": currentIndex,
            "pageSize": 10,
            "couponId": $("#couponId").val(),
            "userId": $("#userId").val()
        };

        $.post("${baseUrl}system/coupon/coupon_get_list", param, function (result) {

            //分页
            new Page({
                parent: $("#copot-page"),
                nowPage: currentIndex,
                pageSize: 10,
                totalCount: result.couponDetailCount,
                setting: {
                    defaultPage: 5,//默认显示可点击分页的个数
                    firstPageText: "首页",
                    prevPageText: "上一页",
                    nextPageText: "下一页",
                    lastPageText: "尾页"
                },
                callback: function (nowPage, totalPage) {
                    coursePage(nowPage);
                }
            });

            $("#couponGetCount").text(result.couponGetCount);
            $("#couponUsedCount").text(result.couponUsedCount);

            var html = "";
            var userId = null;
            var nickName = null;
            var getTime = null;
            var useTime = null;
            var userLevel = null;

            $.each(result.couponDetailList, function (i, data) {

                if(data.userId){
                    userId = data.userId;
                } else {
                    userId = "未领取";
                }
                if(data.nickName){
                    nickName = data.nickName;
                } else {
                    nickName = "未领取";
                }
                if (data.getTime) {
                    getTime = new Date(data.getTime).pattern("yyyy.MM.dd HH:mm");
                } else {
                    getTime = "未领取";
                }
                if (data.useTime) {
                    useTime = new Date(data.useTime).pattern("yyyy.MM.dd HH:mm");
                } else {
                    useTime = "未使用";
                }
                if (data.level == 1) {
                    userLevel = "普通用户";
                }
                if (data.level == 2) {
                    userLevel = "檬会员";
                } else {
                    userLevel = "无数据"
                }

//                html += "<tr><td>" + data.id + "</td>";
                html += "<tr><td>" + data.couponCode + "</td>";
                html += "<td>" + userId + "</td>";
                html += "<td>" + nickName + "</td>";
                html += "<td>" + userLevel + "</td>";
                html += "<td>" + getTime + "</td>";
                html += "<td>" + useTime + "</td>";
                html += "</tr>";
            });
            $("#couponGetList").html(html);

        });

    }


</script>
</@body.page>
</html>